package com.shux.utils.page;

import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

import javax.servlet.http.HttpServletRequest;

/**
 **********************************************
 *  描述：
 * Simba.Hua
 * 2017年5月30日
 **********************************************
**/
public class PageData  extends HashMap<String,Object> implements Map<String,Object> {

    private static final long serialVersionUID = 1L;

    private Map<String,Object> map = null;
    private HttpServletRequest request;

    public PageData(HttpServletRequest request) {
      this.request = request;
      Map<String,String[] > properties = request.getParameterMap();
      
      Map<String,Object> returnMap = new HashMap<String,Object>();
      Iterator<Map.Entry<String,String[]>> entries = properties.entrySet().iterator();
      Map.Entry<String,String[]> entry;
      String name = "";
      String value = "";
      while (entries.hasNext()) {
        entry = (Map.Entry<String,String[]>) entries.next();
        name =  entry.getKey();
        Object valueObj = entry.getValue();
        if (null == valueObj) {
          value = "";
        } else if (valueObj instanceof String[]) {
          String[] values = (String[]) valueObj;
          for (int i = 0; i < values.length; i++) {
            value = values[i] + ",";
          }
          value = value.substring(0, value.length() - 1);
        } else {
          value = valueObj.toString();
        }
        returnMap.put(name, value);
      }
      map =  returnMap;
    }

    public PageData() {
      map = new HashMap<String,Object>();
    }

    @Override
    public Object get(Object key) {
        Object obj = map.get(key);
      if ( obj instanceof Object[]) {
        Object[] arr = (Object[]) obj;
        obj = request == null ?  arr
            : (request.getParameter((String) key) == null ?   arr
                :   arr[0]);
      } else {
        obj = map.get(key);
      }
      return obj;
    }

    public String getString(Object key) {
      return (String) get(key);
    }
    public String[] getStrings(Object key) {
      return (String[]) request.getParameterValues((String) key);
    }

    @SuppressWarnings("unchecked")
    @Override
    public Object put(String key, Object value) {
      return map.put(key, value);
    }

    @Override
    public Object remove(Object key) {
      return map.remove(key);
    }

    public void clear() {
      map.clear();
    }

    public boolean containsKey(Object key) {
      // TODO Auto-generated method stub
      return map.containsKey(key);
    }

    public boolean containsValue(Object value) {
      // TODO Auto-generated method stub
      return map.containsValue(value);
    }

    public Set<Map.Entry<String,Object>> entrySet() {
      // TODO Auto-generated method stub
      return map.entrySet();
    }

    public boolean isEmpty() {
      // TODO Auto-generated method stub
      return map.isEmpty();
    }

    public Set<String> keySet() {
      // TODO Auto-generated method stub
      return map.keySet();
    }

    @SuppressWarnings("unchecked")
    public void putAll(Map<? extends String,? extends Object> t) {
      // TODO Auto-generated method stub
      map.putAll(t);
    }

    public int size() {
      // TODO Auto-generated method stub
      return map.size();
    }

    public Collection<Object> values() {
      // TODO Auto-generated method stub
      return map.values();
    }

    public HttpServletRequest getRequest() {
        return request;
    }

    public void setRequest(HttpServletRequest request) {
        this.request = request;
    }

    
}
